#
# HillTausanovitchMaster.R
#
# Source file to create all tables and figures in
#
# Hill, Seth J. and Chris Tausanovitch. "A Disconnect in Representation? Comparison of Trends in Congressional and Public Polarization."
#
# NOTE: If you just want to use the Hill-Tausanovitch
# estimates of policy ideology for ANES respondents,
# use the file sample_output/HillTausanovitchANESIdeologyEstimates.RData 
# and its readme HillTausanovitchANESIdeologyEstimatesReadme.txt
#

rm( list=ls() )
# Set working directory in location of script for Windows OS.
if (.Platform$OS == "windows") { 
  .doit <- function() { # only works with R.exe; trap errors if using Rscript.exe
  frame_files <- lapply(sys.frames(), function(x) x$ofile);frame_files <- Filter(Negate(is.null), frame_files)
  PATH <- dirname(frame_files[[length(frame_files)]]); setwd(PATH) ; rm(PATH,frame_files)
  }
  try(.doit(),silent=T)
}
require(data.table)
require(bit64)
require(matrixStats)
require(Hmisc)
source("99_prepareFunctions.r")

# =============================
# These scripts create the raw data from the raw ANES Stata files
# downloaded from http://www.electionstudies.org.
# We do not distribute these files, but instead the output created.
# =============================
# Creates full set of data from cumulative file. Result: anes_cdf.RData.
source('01_createANESData.R')
# Merges additional items from 1950s and 1960s cross-sectional files not in cumulative file. Result: anes_cdf_extra_50s60s.RData.
source('02_mergeCrossSectionalItems.R')
# Create version of data with items split apart so as not to glue scaling longer than X years. Result: anes_cdf_extra_50s60s_less_glue.RData
source('03_createLessGluedData.R')
# Create version of data with only social issue items. Result: anes_cdf_extra_50s60s_social_only.RData.
source('04_createSocialItemsOnlyData.R')
# Prepare the data to be passed to JAGS/MCMCPack for each run.
run <- "full" # ANES all questions (including for ordered analysis). Result: allyearsMultinomialFull.RData.
source('05_prepareData.r')
run <- "social" # ANES social issue questions only. Result: allyearsMultinomialSocial.RData.
source('05_prepareData.r')
run <- "unglued" # ANES all questions allowing changing issue meaning. Result: allyearsMultinomialUnglued.RData.
source('05_prepareData.r')

# =============================
# These scripts estimate the Bayesian models.
# They take a lot of computer time to run.
# Resulting files of posterior draws in folder sample_output/:
if (F) {
  source('06_runSenates.r')
  # produces senateOutBig10k.RData
  source('07_RunJointMultinomialFull.r')
  # produces jagsoutFull25kchain1.RData
  source('08_RunJointMultinomialSocial.r')
  # produces jagsoutSocial2k.RData
  source('09_RunJointMultinomialUnglued.r')
  # produces jagsoutUnglued10k.RData
  source('10_RunJointMultinomialFullOrd.r')
  # produces jagsoutFull2kOrd.RData
}
# =============================

# =============================
# These scripts post-process the output of the Bayesian models.
if (F) {
  # This one is for the voter estimates:
  source('11_normJagsOutput.r')
  # And this one is for the Senate:
  source('12_normSenate.r')
}
# =============================

# =============================
# These scripts create tables and figures in the main body.
# =============================
# Figure 1. Set of questions used for analysis.
source("13_makeCumulativeMap.R")
# Figure 2. Distribution of responses to example subset of questions by year.
source("14_marginalsPlot.R")
# Figures 3-9.
run <- "full" # ANES all questions.
source("15_summarizePosteriorSamples.R")
run <- "senate" # Senate.
source("15_summarizePosteriorSamples.R")

# =============================
# These scripts create tables and figures in the on-line appendix.
# =============================

# Table A1. Number of questions and responses per year.
source("16_questionsPerYear.R")
# Figure A1. Dispersion of the estimated distributions over time from the 10-item analysis.
source("17_run_10year_multinomial.r")
source("18_analyze_10year_multinomial.r")
# Figure A2. Densities of point estimates of policy views 1956-2012.
# Figure A3. Estimated densities of policy views, 1956-2012.
cat("Figures A2 and A3 created above via 15_summarizePosteriorSamples.R.\n")
# Power analysis.
source("19_simulatePolarization.R") 
source("20_analyzeSimulations.r") 
# Figures A5-A11: Replication of Figures 3-9.
run <- "ordered" # ANES all questions, ordered logit.
source("15_summarizePosteriorSamples.R")
run <- "social" # ANES social issue questions only.
source("15_summarizePosteriorSamples.R")
run <- "unglued" # ANES all questions allowing changing issue meaning.
source("15_summarizePosteriorSamples.R")
# Figure A12. Set of questions used for data analysis with issue meaning allowed to change over time.
cat("Figure A11 created by createLessGluedData.R.\n")
# Figure A4. Replication of Figure 4 for highly educated voters and those that report trying to influence others
run <- "full"
subset_type <- "education"
source("15_summarizePosteriorSamples.R")
subset_type <- "influence"
source("15_summarizePosteriorSamples.R")

